package com.example.assigment_2;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener,
		OnSeekBarChangeListener {

	private static final String TAG = MainActivity.class.getName();
	private Button changeColor;
	private SeekBar red;
	private SeekBar green;
	private SeekBar blue;

	private TextView redTV;
	private TextView greenTV;
	private TextView blueTV;

	private int redINT;
	private int greenINT;
	private int blueINT;

	private int progressChanged;
	private RelativeLayout lay;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//
		// changeColor = (Button) findViewById(R.id.changeColor);
		// changeColor.setOnClickListener(this);
		//
		// red = (SeekBar) findViewById(R.id.red);
		// green = (SeekBar) findViewById(R.id.green);
		// blue = (SeekBar) findViewById(R.id.blue);
		//
		// red.setMax(0);
		// red.setMax(255);
		// red.setOnSeekBarChangeListener(this);
		//
		// green.setMax(0);
		// green.setMax(255);
		// green.setOnSeekBarChangeListener(this);
		//
		// blue.setMax(0);
		// blue.setMax(255);
		// blue.setOnSeekBarChangeListener(this);
		//
		// redTV = (TextView) findViewById(R.id.redTV);
		// greenTV = (TextView) findViewById(R.id.greenTV);
		// blueTV = (TextView) findViewById(R.id.blueTV);

		// lay = (RelativeLayout) findViewById(R.id.layout);

		Log.e(TAG, " onCreate()");
	}

	@Override
	protected void onStart() {
		super.onStart();
		Log.e(TAG, " onStart()");

	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.e(TAG, " onStop()");

	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.e(TAG, " onPause()");
	}

	@Override
	protected void onDestroy() {
		Log.e(TAG, " onDestroy()");
		super.onDestroy();
	}

	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.changeColor:
			String redHex = Integer.toHexString(redINT);
			if (redHex.length() == 1) {
				redHex = "0" + redHex;
			}

			String greenHex = Integer.toHexString(greenINT);

			if (greenHex.length() == 1) {
				greenHex = "0" + greenHex;
			}

			String blueHex = Integer.toHexString(blueINT);

			if (blueHex.length() == 1) {
				blueHex = "0" + blueHex;
			}

			String color = "#" + redHex + greenHex + blueHex;

			Toast.makeText(getApplicationContext(), color, Toast.LENGTH_LONG)
					.show();

			lay.setBackgroundColor(Color.parseColor(color));

			break;

		default:
			break;
		}

	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		switch (seekBar.getId()) {
		case R.id.red:
			redINT = progress;
			redTV.setText("" + redINT);
			break;
		case R.id.green:
			greenINT = progress;
			greenTV.setText("" + greenINT);
			break;
		case R.id.blue:
			blueINT = progress;
			blueTV.setText("" + blueINT);
			break;
		default:
			break;
		}

		String redHex = Integer.toHexString(redINT);
		if (redHex.length() == 1) {
			redHex = "0" + redHex;
		}

		String greenHex = Integer.toHexString(greenINT);

		if (greenHex.length() == 1) {
			greenHex = "0" + greenHex;
		}

		String blueHex = Integer.toHexString(blueINT);

		if (blueHex.length() == 1) {
			blueHex = "0" + blueHex;
		}

		String color = "#" + redHex + greenHex + blueHex;

		Toast.makeText(getApplicationContext(), color, Toast.LENGTH_LONG)
				.show();

		lay.setBackgroundColor(Color.parseColor(color));

	}

	@Override
	public void onStartTrackingTouch(SeekBar arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStopTrackingTouch(SeekBar arg0) {

	}
}
